
 *** SAMPLE ROBUSTNESS CHECKS ***
 clear 

use "$P_Data_Processed/monthly_station_data_e5_merged.dta"
 	** non-Shell markets

	capture drop Shell_mkt 
	gener Shell_mkt = 0
	replace Shell_mkt = 1 if Brand=="shell"
	sort mktid Shell_mkt
	by mktid: replace Shell_mkt = Shell_mkt[_N]

	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & Shell_mkt==0 ,  absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G1", replace tex(frag) label  dec(3) keep(treat)

	** post 2016 markets
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & year>2016, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G1", append tex(frag) label  dec(3) keep(treat)

	** balanced sample 
	capture drop n_station_obs
	sort StID year_month
	by StID: gener n_station_obs = _N
	ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_station_obs==36, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G1", append tex(frag) label dec(3) keep(treat)
	
	* balanced sample with only markets that don't change
	capture drop non_changing_mkts
	capture drop sd_mkt_n_stations
	by mktid, sort: egen sd_mkt_n_stations = sd(n_act_stations)
	gener non_changing_mkts =(sd_mkt_n_stations==0)
		ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & non_changing_mkts==1 & n_station_obs==36, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_G1", append tex(frag) label dec(3) keep(treat)

** dropping highway stations 


use "$P_Data_Processed/monthly_station_data_e5.dta", clear

merge m:1 StID using  "$P_Data_Processed/highway_stations.dta"
drop if _merge==2
drop if _merge==3
drop _merge 


by year_month Post, sort: gener postal_n_stations = _N
gener postal_n_others = postal_n_stations-1
drop if Post==0

** defining treatment 

capture drop treat
gener treat = month_treat_period*treat_group
label variable treat "Adopter" 

**
** drop outlier observations (top and bottom 1%)
drop if mean_wh_margin<0
drop if mean_wh_margin>0.2
drop if mean_price<1.18
drop if mean_price>1.7
	

sort StID year_month
by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0
sort StID first_treat_month
by StID: replace first_treat_month = first_treat_month[1]
replace first_treat_month = 1000 if missing(first_treat_month)
	
* number of other adopters in your Postal code
by Post year_month, sort: egen n_compet_adopt = total(treat)
replace n_compet_adopt = n_compet_adopt - 1 if treat==1	
	
** generating simple IV - share of adopters in your brand
by Brand year_month, sort: gener n_brand = _N
by Brand year_month: egen n_treat = total(treat)

gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1
label variable n_brand "N Brand Stations"
label variable share_others_treated "Share Brand Adopters"
label variable postal_n_stations "N Competitors in ZIP"
label variable n_compet_adopt "N Competitors Adopting"

	
		
merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener station = 1 
gegen n_act_stations = total(station), by(mktid year_month)


*by year_month mktid, sort: gener mkt_n_stations = _N
gegen n_mkt_compet_adopt = total(treat), by(year_month mktid)
replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if treat==1

capture drop n_months_since_treat 
sort StID year_month
capture drop age 
by StID: gener age = _n
gener n_months_since_treat = year_month - first_treat_month
replace n_months_since_treat=. if n_months_since_treat<-31

ivreghdfe mean_wh_margin n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019, absorb(year_month StID) cluster(mktid) 
outreg2 using  "$P_Tables/Table_G1", append tex(frag) label dec(3) keep(treat)

